{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Comparison of Two Electron Integral Speed"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "import numpy as np\n",
    "from matplotlib import pylab as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyquante2.ctwo import vrr,vrr_recursive\n",
    "from pyquante2.ints.hgp import vrr as pyvrr\n",
    "from pyquante2.ints.hgp import hrr as pyhrr"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Test the speed of python vs C 2e integral code. Ideally also compare to Julia code.\n",
    "\n",
    "In particular, we want to compare the Vertical Recurrence Relationship (VRR) code on primitive basis functions between Python, C, and Julia codes. \n",
    "\n",
    "The call signature for vrr looks like:\n",
    "\n",
    "    def vrr(xyza,norma,lmna,alphaa,\n",
    "        xyzb,normb,alphab,\n",
    "        xyzc,normc,lmnc,alphac,\n",
    "        xyzd,normd,alphad,M):\n",
    "        \n",
    "Some of the variables need to be unpacked as tuples:\n",
    "\n",
    "    la,ma,na = lmna\n",
    "    lc,mc,nc = lmnc\n",
    "    xa,ya,za = xyza\n",
    "    xb,yb,zb = xyzb\n",
    "    xc,yc,zc = xyzc\n",
    "    xd,yd,zd = xyzd\n",
    "    \n",
    "We want to explore the impact of all of these variables on the performance, as well as the M variable.\n",
    "\n",
    "Let's simplify everything by setting all of the other variables to 1.0:\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## s orbitals on the same center:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "748 ns ± 1.18 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)\n"
     ]
    }
   ],
   "source": [
    "#           xyza   na  lmna   aa  xyzb   nb ab  xyzc   nc  lmnc   ac  xyzd   nd ad  M\n",
    "%timeit vrr(0,0,0, 1., 0,0,0, 1., 0,0,0, 1.,1., 0,0,0, 1., 0,0,0, 1., 0,0,0, 1.,1., 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "def d(x,y,z): return np.array((x,y,z),'d')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "49.1 µs ± 794 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n"
     ]
    }
   ],
   "source": [
    "#           xyza   na  lmna   aa  xyzb   nb ab  xyzc   nc  lmnc   ac  xyzd   nd ad  M\n",
    "%timeit pyvrr(d(0,0,0), 1., (0,0,0), 1., d(0,0,0), 1.,1., d(0,0,0), 1., (0,0,0), 1., d(0,0,0), 1.,1., 0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## sp integral, same center"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "959 ns ± 2.47 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)\n"
     ]
    }
   ],
   "source": [
    "#           xyza   na  lmna   aa  xyzb   nb ab  xyzc   nc  lmnc   ac  xyzd   nd ad  M\n",
    "%timeit vrr(0,0,0, 1., 0,0,0, 1., 0,0,0, 1.,1., 0,0,0, 1., 1,0,0, 1., 0,0,0, 1.,1., 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "978 ns ± 7.94 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)\n"
     ]
    }
   ],
   "source": [
    "#           xyza   na  lmna   aa  xyzb   nb ab  xyzc   nc  lmnc   ac  xyzd   nd ad  M\n",
    "%timeit vrr(0,0,0, 1., 0,0,0, 1., 0,0,0, 1.,1., 0,0,0, 1., 0,1,0, 1., 0,0,0, 1.,1., 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "58.8 µs ± 1.66 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n"
     ]
    }
   ],
   "source": [
    "#           xyza   na  lmna   aa  xyzb   nb ab  xyzc   nc  lmnc   ac  xyzd   nd ad  M\n",
    "%timeit pyvrr(d(0,0,0), 1., (0,0,0), 1., d(0,0,0), 1.,1., d(0,0,0), 1., (1,0,0), 1., d(0,0,0), 1.,1., 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "57.1 µs ± 542 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n"
     ]
    }
   ],
   "source": [
    "#           xyza   na  lmna   aa  xyzb   nb ab  xyzc   nc  lmnc   ac  xyzd   nd ad  M\n",
    "%timeit pyvrr(d(0,0,0), 1., (0,0,0), 1., d(0,0,0), 1.,1., d(0,0,0), 1., (0,1,0), 1., d(0,0,0), 1.,1., 0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## s intgrals, 4 different centers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "961 ns ± 1.51 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)\n"
     ]
    }
   ],
   "source": [
    "#           xyza   na  lmna   aa  xyzb   nb ab  xyzc   nc  lmnc   ac  xyzd   nd ad  M\n",
    "%timeit vrr(0,0,0, 1., 0,0,0, 1., 0,0,1., 1.,1., 0,1.,0, 1., 0,0,0, 1., 1.,1.,0, 1.,1., 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "70.9 µs ± 346 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n"
     ]
    }
   ],
   "source": [
    "#           xyza   na  lmna   aa  xyzb   nb ab  xyzc   nc  lmnc   ac  xyzd   nd ad  M\n",
    "%timeit pyvrr(d(0,0,0), 1., (0,0,0), 1., d(0,0,1), 1.,1., d(0,1,0), 1., (0,0,0), 1., d(1,1,0), 1.,1., 0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## sp integrals, 4 different centers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.22 µs ± 8.25 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)\n"
     ]
    }
   ],
   "source": [
    "#           xyza   na  lmna   aa  xyzb   nb ab  xyzc   nc  lmnc   ac  xyzd   nd ad  M\n",
    "%timeit vrr(0,0,0, 1., 0,0,0, 1., 0,0,1., 1.,1., 0,1.,0, 1., 1,0,0, 1., 1.,1.,0, 1.,1., 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "66.2 µs ± 1.14 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n"
     ]
    }
   ],
   "source": [
    "#             xyza   na     lmna     aa    xyzb   nb ab  xyzc   nc  lmnc   ac  xyzd   nd ad  M\n",
    "%timeit pyvrr(d(0,0,0), 1., (0,0,0), 1., d(0,0,1), 1.,1., d(0,1,0), 1., (1,0,0), 1., d(1,1,0), 1.,1., 0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## pd integrals, 4 different centers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.22 µs ± 24.3 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n"
     ]
    }
   ],
   "source": [
    "#           xyza   na  lmna   aa  xyzb   nb ab  xyzc   nc  lmnc   ac  xyzd   nd ad  M\n",
    "%timeit vrr(0,0,0, 1., 1,0,0, 1., 0,0,1., 1.,1., 0,1.,0, 1., 1,1,0, 1., 1.,1.,0, 1.,1., 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "95.9 µs ± 1.65 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n"
     ]
    }
   ],
   "source": [
    "#             xyza   na     lmna     aa    xyzb   nb ab  xyzc   nc  lmnc   ac  xyzd   nd ad  M\n",
    "%timeit pyvrr(d(0,0,0), 1., (1,0,0), 1., d(0,0,1), 1.,1., d(0,1,0), 1., (1,1,0), 1., d(1,1,0), 1.,1., 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
